# -*- encoding=UTF-8 -*-
from app import app, db
from flask import url_for, render_template, request, abort, redirect
from app.models import ResultVideoDetail, ResultVideoSum, ResultInterfaceDetail, ResultInterfaceSum
from sqlalchemy import and_, or_, not_

per_page=5


@app.route("/")
def index():
    return render_template("home.html")

@app.route("/favicon.ico")
def ico():
    return app.send_static_file('images/favicon.ico')

@app.route("/saveRemark/")
def saveRemark():
    type=request.args.get("type", None)
    sum_id=request.args.get("id", None)
    remark=request.args.get("remark", "")
    print(f"type:{type}, id:{id}, remark:{remark}")
    if type == "video":
        ResultVideoSum.query.filter_by(id=sum_id).update({"remark":remark})
        db.session.commit()
        return {"code":200, "message":"save remark successfully"}
    elif type == "interface":
        ResultInterfaceSum.query.filter_by(id=sum_id).update({"remark":remark})
        db.session.commit()
        return {"code": 200, "message": "save remark successfully"}
    else:
        return {"code":404, "message": f"no {type} relevant page"}


@app.route("/sum/")
def sum():
    type=request.args.get("type", "video")
    page=int(request.args.get("page", 1))
    test_result=request.args.get("test_result", "")
    if type == "video":
        failed_test_cases=db.session.query(ResultVideoDetail.result_sum_id, ResultVideoDetail.event_type_id, db.func.count('*')).filter(ResultVideoDetail.test_result != 1).group_by(ResultVideoDetail.result_sum_id, ResultVideoDetail.event_type_id).all()
        each_test_summary={}
        for each in list(failed_test_cases):
            print(each)
            if each[0] in each_test_summary.keys():
                each_test_summary[each[0]].append(list(each[1:]))
            else:
                each_test_summary[each[0]]=[list(each[1:])]
        print(each_test_summary)
        pagination_instance = ResultVideoSum.query.paginate(page=page, per_page=int(per_page), error_out=False)
        pages = pagination_instance.iter_pages(left_edge=2, left_current=2, right_current=2, right_edge=2)
        return render_template("pageVideoSum.html", pagination_instance=pagination_instance, pages=list(pages), type=type, page=pagination_instance.pages, each_test_summary=each_test_summary)
    elif type == "interface":
        if test_result == "":
            pagination_instance=ResultInterfaceSum.query.paginate(page=page, per_page=per_page, error_out=False)
            pages=pagination_instance.iter_pages(left_edge=2, left_current=2, right_current=2, right_edge=2)
            return render_template("pageInterfaceSum.html", pagination_instance=pagination_instance, pages=list(pages), type=type, page=pagination_instance.pages)
        else:
            pass
    else:
        return abort(404)

@app.route("/detail/")
def detail():
    type = request.args.get("type", "video")
    sum_id=int(request.args.get("sum_id", 1))
    page=int(request.args.get("page", 1))
    condition=request.args.get("condition", "")
    if type == "video":
        if condition=="":
            pagination_instance = ResultVideoDetail.query.filter_by(result_sum_id=sum_id).paginate(page=page, per_page=int(per_page), error_out=False)
            pages = pagination_instance.iter_pages(left_edge=2, left_current=2, right_current=2, right_edge=2)
            if pagination_instance.pages < page and pagination_instance.pages > 0:
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page={pagination_instance.pages}")
            elif pagination_instance.pages == 0:
                return render_template("pageVideoDetail.html", pagination_instance=pagination_instance, pages=list(pages), type=type, sum_id=sum_id, page=1)
            elif page < 1:
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page=1")
            else:
                return render_template("pageVideoDetail.html", pagination_instance=pagination_instance, pages=list(pages), type=type, sum_id=sum_id, page=page)
        else:
            if condition == "1":
                pagination_instance = ResultVideoDetail.query.filter_by(result_sum_id=sum_id).filter(ResultVideoDetail.expect_result == ResultVideoDetail.actual_result).paginate(page=page, per_page=int(per_page), error_out=False)
            elif condition == "0":
                pagination_instance = ResultVideoDetail.query.filter_by(result_sum_id=sum_id).filter(ResultVideoDetail.expect_result != ResultVideoDetail.actual_result).paginate(page=page, per_page=int(per_page), error_out=False)
            elif condition == "-1":
                pagination_instance = ResultVideoDetail.query.filter_by(result_sum_id=sum_id).filter(or_(ResultVideoDetail.expect_result==None, ResultVideoDetail.actual_result==None)).paginate(page=page, per_page=int(per_page), error_out=False)
            else:
                print(f"condition is not 1, 0 or -1")
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page=1")
            pages = pagination_instance.iter_pages(left_edge=2, left_current=2, right_current=2, right_edge=2)
            print(f"pagination_instance.pages: {pagination_instance.pages}")
            if pagination_instance.pages < page and pagination_instance.pages > 0:
                print(f"redirect for larger than max")
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page={pagination_instance.pages}&condition={condition}")
            elif pagination_instance.pages == 0:
                return render_template("pageVideoDetail.html", pagination_instance=pagination_instance, pages=list(pages), type=type, sum_id=sum_id, page=1, condition=condition)
            elif page < 1:
                print(f"redirect for less than min")
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page=1&condition={condition}")
            else:
                return render_template("pageVideoDetail.html", pagination_instance=pagination_instance, pages=list(pages), type=type, sum_id=sum_id, page=page, condition=condition)
    elif type == "interface":
        if condition == "":
            pagination_instance=ResultInterfaceDetail.query.paginate(page=page, per_page=per_page, error_out=False)
            pages = pagination_instance.iter_pages(left_edge=2, left_current=2, right_current=2, right_edge=2)
            if page > pagination_instance.pages and pagination_instance.pages > 0:
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page={pagination_instance.pages}")
            elif pagination_instance.pages == 0:
                return render_template("pageInterfaceDetail.html", pagination_instance=pagination_instance, pages=list(pages), type=type, sum_id=sum_id, page=1)
            elif page < 1:
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page=1")
            else:
                return render_template("pageInterfaceDetail.html", pagination_instance=pagination_instance, pages=list(pages), type=type, sum_id=sum_id, page=page)
        else:
            if condition == "1":
                pagination_instance = ResultInterfaceDetail.query.filter(and_(or_(ResultInterfaceDetail.return_code_compare_result ==None, ResultInterfaceDetail.return_code_compare_result==1), or_(ResultInterfaceDetail.structure_complete_compare_result==None, ResultInterfaceDetail.structure_complete_compare_result==1), or_(ResultInterfaceDetail.param_complete_compare_result==None, ResultInterfaceDetail.param_complete_compare_result==1))).filter(not_(and_(ResultInterfaceDetail.return_code_compare_result==None, ResultInterfaceDetail.structure_complete_compare_result==None, ResultInterfaceDetail.param_complete_compare_result==None, ResultInterfaceDetail.param_value_compare_result==None))).paginate(page=page, per_page=per_page, error_out=False)
            elif condition == "0":
                pagination_instance = ResultInterfaceDetail.query.filter(or_(ResultInterfaceDetail.return_code_compare_result==0, ResultInterfaceDetail.structure_complete_compare_result==0, ResultInterfaceDetail.param_complete_compare_result==0, ResultInterfaceDetail.param_value_compare_result==0)).paginate(page=page, per_page=per_page, error_out=False)
            elif condition == "-1":
                pagination_instance = ResultInterfaceDetail.query.filter(and_(ResultInterfaceDetail.return_code_compare_result==None, ResultInterfaceDetail.structure_complete_compare_result==None, ResultInterfaceDetail.param_complete_compare_result==None, ResultInterfaceDetail.param_value_compare_result==None)).paginate(page=page, per_page=per_page, error_out=False)
            else:
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page=1")
            pages = pagination_instance.iter_pages(left_edge=2, left_current=2, right_current=2, right_edge=2)
            if page > pagination_instance.pages and pagination_instance.pages > 0:
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page={pagination_instance.pages}&condition={condition}")
            elif pagination_instance.pages == 0:
                return render_template("pageInterfaceDetail.html", pagination_instance=pagination_instance, pages=list(pages), type=type, sum_id=sum_id, page=1, condition=condition)
            elif page < 1:
                return redirect(f"/detail/?type={type}&sum_id={sum_id}&page=1&condition={condition}")
            else:
                return render_template("pageInterfaceDetail.html", pagination_instance=pagination_instance, pages=list(pages), type=type, sum_id=sum_id, page=page, condition=condition)
    else:
        abort(404)

# @app.errorhandler(500)
@app.errorhandler(404)
def not_found(error):
    url=request.url
    print("im here")
    return render_template("not_found.html", url=url)

if __name__=="__main__":
    pass
